#pragma once
#include "ParamterSetting.h"
#include "ParamterSetting2.h"
#include "StringHelper.h"

class ParamterSettingGroup
{
public:
	ParamterSetting* dlg;
	ParamterSetting2* dlg2;

	CDialog* pCurrentDlg;

	vector<int>* pParams1;
	vector<int>* pParams21;
	vector<double>* pParams22;

	void DoSwitch()
	{
		if (Inited())
		{
			if (pCurrentDlg)
			{
				if ( pCurrentDlg == dlg)
					pCurrentDlg = dlg2;
				else if (pCurrentDlg == dlg2)
					pCurrentDlg = dlg;
			}
			else
				pCurrentDlg = dlg;

			ShowCurrent();
		}
	}

	void ShowCurrent()
	{
		//if (dlg)
		//	dlg->ShowWindow(SW_SHOW);
		if (dlg)
			dlg->ShowWindow(SW_HIDE);
		if (dlg2)
			dlg2->ShowWindow(SW_HIDE);

		if (pCurrentDlg)
			pCurrentDlg->ShowWindow(SW_NORMAL);
	}

	void DoSave()
	{
		//if (dlg) dlg->OnSave(),delete dlg;
		//if (dlg2) dlg2->OnSave(),delete dlg2;
		CStringHelper* pstr = new CStringHelper;
		CStringHelper* pstr2 = new CStringHelper;
		vector<CString> pageInts1 = CStringHelper::TransferFromValues2Strs(dlg->GetPageParameters());
		vector<CString> pageInts2 = CStringHelper::TransferFromValues2Strs(dlg2->GetPageParameters());
		vector<CString> pageDoubles = CStringHelper::TransferFromValues2Strs(dlg2->GetPageParameters2());

		if (pParams1)
			*pParams1 = GetIntFromString(pageInts1);
		if (pParams21)
			*pParams21 = GetIntFromString(pageInts2);
		if (pParams22)
			*pParams22 = GetDoubleFromString(pageDoubles);

		pstr->Format(&pageInts1,',',true);
		pstr2->Format(&pageInts2,',');
		pstr2->Format(&pageDoubles,',',true);

		CString look = *pstr;
		look = CStringHelper::Base64Encode(look);

		CString look2 = *pstr2;
		look2 = CStringHelper::Base64Encode(look2);
		
		pstr->WriteStr("test.txt",look);
		pstr2->WriteStr("test2.txt",look2);
		//pstr->Format();
		//pstr->TestSerialize();
	}

	bool Inited(){
		return dlg != NULL && dlg2 != NULL;
	}

	void Init(CWnd* pParent)
	{
		dlg = new ParamterSetting();
		dlg->Create(ParamterSetting::IDD,pParent);
		dlg2 = new ParamterSetting2();
		dlg2->Create(ParamterSetting2::IDD,pParent);

		if (pParams1==NULL)
			pParams1 = new vector<int>();
		if (pParams21 == NULL)
			pParams21 = new vector<int>();
		if (pParams22 == NULL)
			pParams22 = new vector<double>();
	}

	ParamterSettingGroup()
		:dlg(NULL)
		,dlg2(NULL)
		,pCurrentDlg(NULL)
		,pParams1(new vector<int>())
		,pParams21(new vector<int>())
		,pParams22(new vector<double>())
	{
		DoSerialize(true);
	}


	ParamterSettingGroup(CWnd* pParent)
		:dlg(NULL)
		,dlg2(NULL)
		,pCurrentDlg(NULL)
		,pParams1(new vector<int>())
		,pParams21(new vector<int>())
		,pParams22(new vector<double>())
	{
#ifdef RUNMYTEST
		CStringHelper* pStrHelper = new CStringHelper();
		pStrHelper->Test();
		if (pStrHelper) delete pStrHelper;
#endif
		dlg = new ParamterSetting();
		dlg->Create(ParamterSetting::IDD,pParent);
		dlg2 = new ParamterSetting2();
		dlg2->Create(ParamterSetting2::IDD,pParent);
	
		if (Inited())
			DoSerialize();
	}
	~ParamterSettingGroup(void)
	{
		dlg = 0;
		dlg2 = 0;
	}

	bool ParametersReaded()
	{
		if (pParams1->size() == 35
			&& pParams21->size() ==13
			&& pParams22->size() ==20)
		{
			return true;
		}
		return false;
	}

	void DoSerialize(bool noImportToDlg=false)
	{
		CStringHelper* pstr = new CStringHelper;

		CStringHelper temp(pstr->ReadStr("test.txt"));
		CStringHelper temp2(CStringHelper::Base64Decode(temp));
		vector<CString>* paramsCanRead = temp2.SplitString(',');
		
		CStringHelper temp3(pstr->ReadStr("test2.txt"));
		CStringHelper temp4(CStringHelper::Base64Decode(temp3));
		vector<CString>* paramsCanRead2 = temp4.SplitString(',');

		if (paramsCanRead->empty() == false && paramsCanRead->size() == 37
			&& paramsCanRead2->empty() == false && paramsCanRead2->size() == 33)
		{
			vector<CString> temp1,temp21,temp22;
			temp1.assign( paramsCanRead->begin(),paramsCanRead->begin()+37);
			temp21.assign(paramsCanRead2->begin(),paramsCanRead2->begin()+13);
			temp22.assign(paramsCanRead2->begin()+13,paramsCanRead2->end());
			
			*pParams1 = GetIntFromString(temp1);
			*pParams21 = GetIntFromString(temp21);
			*pParams22 = GetDoubleFromString(temp22);
			
			if (!noImportToDlg)
			{
				if (dlg != NULL)
				{
					dlg->SetPageParameters(temp1);
				}
				if (dlg2 != NULL)
				{
					dlg2->SetPageParameters(temp21);
					dlg2->SetPageParameters2(temp22);
				}
			}
		}
		else if (paramsCanRead->empty() == false && paramsCanRead->size() == 35
			&& paramsCanRead2->empty() == false && paramsCanRead2->size() == 33)
		{
			vector<CString> temp1,temp21,temp22;
			temp1.assign( paramsCanRead->begin(),paramsCanRead->begin()+35);
			temp21.assign(paramsCanRead2->begin(),paramsCanRead2->begin()+13);
			temp22.assign(paramsCanRead2->begin()+13,paramsCanRead2->end());

			*pParams1 = GetIntFromString(temp1);
			*pParams21 = GetIntFromString(temp21);
			*pParams22 = GetDoubleFromString(temp22);

			if (!noImportToDlg)
			{
				if (dlg != NULL)
				{
					dlg->SetPageParameters(temp1);
				}
				if (dlg2 != NULL)
				{
					dlg2->SetPageParameters(temp21);
					dlg2->SetPageParameters2(temp22);
				}
			}
		}
		//vector<CString> pageInts1 = CStringHelper::TransferFromValues2Strs(dlg->GetPageParameters());
		//vector<CString> pageInts2 = CStringHelper::TransferFromValues2Strs(dlg2->GetPageParameters());
		//vector<CString> pageDoubles = CStringHelper::TransferFromValues2Strs(dlg2->GetPageParameters2());
		//pstr->Format(&pageInts1,',');
		//pstr->Format(&pageInts2,',');
		//pstr->Format(&pageDoubles,',',true);

		//CString look = *pstr;
		//look = CStringHelper::Base64Encode(look);
		//pstr->WriteStr("D:\\test.txt",look);
	}

	static vector<int> GetIntFromString(vector<CString>& strs)
	{
		vector<int> ints;
		for (vector<CString>::iterator iter = strs.begin()
			  ; iter != strs.end()
			  ; ++iter)
		{
			int i = atoi(*iter);
			ints.push_back(i);
		}
		return ints;
	}

	static vector<double> GetDoubleFromString(vector<CString>& strs)
	{
		vector<double> ints;
		for (vector<CString>::iterator iter = strs.begin()
			; iter != strs.end()
			; ++iter)
		{
			double i = atof(*iter);
			ints.push_back(i);
		}
		return ints;
	}
};
